package com.kerrykidz.system.util.tag;

import java.io.IOException;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class TokenTag implements Tag {

	private PageContext pageContext;

	private Tag parent;

	@Override
	public int doEndTag() throws JspException {
		HttpSession session = this.pageContext.getSession();
		String token = TokenUtils.createToken(session);
		String tag = "<input type=\"hidden\" name=\"" + TokenUtils.TOKEN_SESSION_KEY + "\" value=\"" + token + "\" />";
		try {
			this.pageContext.getOut().write(tag);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	@Override
	public int doStartTag() throws JspException {
		return SKIP_BODY;
	}

	@Override
	public Tag getParent() {
		return this.parent;
	}

	@Override
	public void release() {

	}

	@Override
	public void setPageContext(PageContext arg0) {
		this.pageContext = arg0;
	}

	@Override
	public void setParent(Tag arg0) {
		this.parent = arg0;
	}

}
